package com.usooft.metaTalent.admin.system;

import com.usooft.framework.core.utils.MapBuilder;
import com.usooft.framework.web.auth.RequiresAuthentication;
import com.usooft.metaTalent.core.system.entity.Employee;
import com.usooft.metaTalent.core.system.service.EmployeeRoleService;
import com.usooft.metaTalent.core.system.service.RolePermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/profile")
@RequiredArgsConstructor
public class ProfileController {

    private final EmployeeRoleService employeeRoleService;
    private final RolePermissionService rolePermissionService;

    @GetMapping
    @RequiresAuthentication
    public ResponseEntity<?> index(Employee employee) {
        Set<Long> roleIds = employeeRoleService.listRoleIds(employee);
        Set<String> permissions = roleIds
                .stream()
                .flatMap(roleId -> rolePermissionService.listPermissionsByRoleId(roleId).stream())
                .collect(Collectors.toSet());
        return ResponseEntity.ok(MapBuilder.builder()
                .put("id", employee.getId())
                .put("admin", employee.getAdmin())
                .put("name", employee.getName())
                .put("permissions", permissions)
                .build());
    }

}
